﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using QLNHK.BLL;
using Infragistics.Win.UltraWinGrid;

namespace QLNHK
{
    public partial class FrmChonHoKhau : FrmBase
    {
        public int _TinhID;
        public int _QuanHuyenID;
        public int _XaPhuongIDP;
        public int _ToID;
        public int _SoSoHoKhau;

        private DataSet dsData = new DataSet();
        private string whereCondition, orderBy;
        private int tongSoNhanKhau, tongSoHoKhau;

        public FrmChonHoKhau()
        {
            InitializeComponent();
        }
        private void FrmChonHoKhau_Load(object sender, EventArgs e)
        {
            btnTimKiem.PerformClick();
        }

        private void cboThanhPho_ValueChanged(object sender, EventArgs e)
        {
            if (cboThanhPho.Value != null)
            {
                int cboThanhPhoValue = 0;
                Int32.TryParse(cboThanhPho.Value.ToString(), out cboThanhPhoValue);
                if (cboThanhPhoValue > 0)
                    Global.setDataToComboQuanHuyen(cboQuanHuyen, Convert.ToInt32(cboThanhPho.Value));
            }
        }
        private void cboQuanHuyen_ValueChanged(object sender, EventArgs e)
        {
            if (cboQuanHuyen.Value != null)
            {
                int cboQuanHuyenValue = 0;
                Int32.TryParse(cboQuanHuyen.Value.ToString(), out cboQuanHuyenValue);
                if (cboQuanHuyenValue > 0)
                    Global.setDataToComboXaPhuong(cboXaPhuong, Convert.ToInt32(cboQuanHuyen.Value));
            }
        }
        private void cboXaPhuong_ValueChanged(object sender, EventArgs e)
        {
            if (cboXaPhuong.Value != null)
            {
                int cboXaPhuongValue = 0;
                Int32.TryParse(cboXaPhuong.Value.ToString(), out cboXaPhuongValue);
                if (cboXaPhuongValue > 0)
                    Global.setDataToComboToDanPho(cboToDanPho, Convert.ToInt32(cboXaPhuong.Value));
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            try
            {
                txtSoSoHK.Text = txtHoTen.Text = string.Empty;
                //Set dia ban
                Global.setDataToComboDanhMuc(cboLoaiHoKhau, DMCatalogue.DM_LOAIHOKHAU);
                Global.setDataToComboThanhPho(cboThanhPho);
                cboThanhPho.Value = _TinhID;
                cboQuanHuyen.Value = _QuanHuyenID;
                cboXaPhuong.Value = _XaPhuongIDP;
                cboToDanPho.Value = _ToID;
                txtHoTen.Focus();
            }
            catch (Exception ex) { Logger.LocalLogger.Instance().WriteMessage(ex); }
        }
        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            btnClear.PerformClick();

            LoadData();
        }
        private void LoadData()
        {
            try
            {
                Cursor = Cursors.WaitCursor;

                //Load data
                whereCondition = "1=1";
                //So so ho khau
                if (txtSoSoHK.Text.Trim().Length > 0)
                {
                    if (chkTimChinhXac.Checked)
                        whereCondition += " AND SoSoHoKhau = " + txtSoSoHK.Text.Trim();
                    else
                        whereCondition += " AND SoSoHoKhau like '%" + txtSoSoHK.Text.Trim() + "%'";
                }
                //Ho ten
                if (txtHoTen.Text.Trim().Length > 0)
                {
                    if (chkTimChinhXac.Checked)
                        whereCondition += " AND (HoLot = N'" + txtHoTen.Text.Trim() + "' OR Ten = N'" + txtHoTen.Text.Trim() + "')";
                    else
                        whereCondition += " AND (HoLot like N'%" + txtHoTen.Text.Trim() + "%' OR Ten like N'%" + txtHoTen.Text.Trim() + "%')";
                }

                if (System.Convert.ToInt32(cboThanhPho.Value) > 0)
                    whereCondition += string.Format(" AND (TinhTPID = {0})", cboThanhPho.Value);
                if (System.Convert.ToInt32(cboQuanHuyen.Value) > 0)
                    whereCondition += string.Format(" AND (QuanHuyenID = {0})", cboQuanHuyen.Value);
                if (System.Convert.ToInt32(cboXaPhuong.Value) > 0)
                    whereCondition += string.Format(" AND (PhuongXaID = {0})", cboXaPhuong.Value);
                if (System.Convert.ToInt32(cboToDanPho.Value) > 0)
                    whereCondition += string.Format(" AND (ToDanPhoID = {0})", cboToDanPho.Value);

                orderBy = "SoSoHoKhau asc";
                dsData = HoKhau.SearchBy(whereCondition, orderBy);
                gridHoKhau.DataSource = dsData.Tables[0];

                tongSoHoKhau = dsData.Tables[0].Rows.Count;
                // Declare an object variable.
                object sumObject;
                sumObject = dsData.Tables[0].Compute("Sum(SoNhanKhau)", "");
                lblTongSo.Text = string.Format("Tổng số hộ khẩu: {0} - nhân khẩu: {1}", tongSoHoKhau, (sumObject == System.DBNull.Value ? "0" : sumObject.ToString()));
            }
            catch (Exception ex)
            {
                Logger.LocalLogger.Instance().WriteMessage(ex);
                MessageBox.Show(ex.Message);
            }
            finally { Cursor = Cursors.Default; }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnChon_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridHoKhau.ActiveRow != null && gridHoKhau.ActiveRow.Cells["ID"].Value != null)
                {
                    //Get SoSoHoKhau nhan khau
                    _SoSoHoKhau = System.Convert.ToInt32(gridHoKhau.ActiveRow.Cells["SoSoHoKhau"].Value);
                    DialogResult = DialogResult.OK;
                }
            }
            catch (Exception ex) { Logger.LocalLogger.Instance().WriteMessage(ex); }
        }

        private void gridHoKhau_InitializeLayout(object sender, Infragistics.Win.UltraWinGrid.InitializeLayoutEventArgs e)
        {
            try
            {
                e.Layout.Override.CellClickAction = CellClickAction.RowSelect;

                UltraGridBand band = e.Layout.Bands[0];
                band.Columns["ID"].Hidden = true;
                band.Columns["CanBoQuanLyID"].Hidden = true;
                band.Columns["ThuongTru"].Header.Caption = "Nguyên quán/ Quê quán";

                band.Columns["LoaiHoKhauID"].ValueList = Global.getValueListDanhMuc(DMCatalogue.DM_LOAIHOKHAU);
                band.Columns["LoaiHoKhauID"].Style = Infragistics.Win.UltraWinGrid.ColumnStyle.DropDownList;
                band.Columns["LoaiHoKhauID"].CellActivation = Activation.NoEdit;

                band.Columns["SoSoHoKhau"].CellAppearance.TextHAlign = Infragistics.Win.HAlign.Center;
                band.Columns["HSHKSo"].CellAppearance.TextHAlign = Infragistics.Win.HAlign.Center;
                band.Columns["SoNhanKhau"].CellAppearance.TextHAlign = Infragistics.Win.HAlign.Center;
            }
            catch (Exception ex) { Logger.LocalLogger.Instance().WriteMessage(ex); }
        }
    }
}
